﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Telerik.Web.UI;
using System.IO;

namespace EXAM.Admin.Page_Admin
{
    public partial class GadgetDef : System.Web.UI.Page
    {
        string sErr = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
            }
        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            RefreshForm("");
        }
        private void RefreshForm(string val)
        {
            txtID.Text = val;
            txtName.Text = val;
            cboType.Text = val;
            txtHeigth.Value = 100;
            txtWidth.Value = 300;
            imgPic.ImageUrl = "";
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            DTO.GadgetInfo usrInf = new DTO.GadgetInfo();
            usrInf = GetDataFromForm(usrInf);

            BUS.GadgetControl ctr = new BUS.GadgetControl();
            if (txtID.Text != "" && txtID.Text != "-1")
            {
                sErr = ctr.Update(usrInf);
                if (sErr != "")
                    lbErr.Text = sErr;
                else
                    lbErr.Text = "Update successfull";
            }
            else
            {
                txtID.Text = ctr.Add(usrInf, ref sErr).ToString();
                if (sErr != "")
                    lbErr.Text = sErr;
                else
                    lbErr.Text = "Insert successfull";
            }
            dgvList.Rebind();
            //if (upPicture.UploadedFiles.Count > 0)
            //{
            //    imgPic.ImageUrl = upPicture.TargetFolder + "/" + Path.GetFileName(upPicture.UploadedFiles[0].FileName);
            //}
        }

        private DTO.GadgetInfo GetDataFromForm(DTO.GadgetInfo usrInf)
        {
            if (upPicture.UploadedFiles.Count > 0)
            {
                imgPic.ImageUrl = upPicture.TargetFolder + "/" + Path.GetFileName(upPicture.UploadedFiles[0].FileName);
            }
            if (imgPic.ImageUrl != "")
            {
                usrInf.Image = Path.GetFileName(imgPic.ImageUrl);
            }
            if (txtID.Text != "" && txtID.Text != "-1")
                usrInf.ID = Convert.ToInt32(txtID.Text);
            usrInf.Title = txtName.Text;

            usrInf.Type = cboType.Text;
            usrInf.Height = Convert.ToInt32(txtHeigth.Value);
            usrInf.Width = Convert.ToInt32(txtWidth.Value);

            return usrInf;
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            foreach (GridItem item in dgvList.SelectedItems)
            {
                if (item.ItemIndex >= 0)
                {
                    string id = item.OwnerTableView.DataKeyValues[item.ItemIndex]["ID"].ToString();
                    BUS.GadgetControl ctr = new BUS.GadgetControl();
                    sErr = ctr.Delete(Convert.ToInt32(id));
                }
            }
            dgvList.Rebind();
        }

        protected void dgvList_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            BUS.GadgetControl ctr = new BUS.GadgetControl();
            dgvList.DataSource = ctr.GetAll(ref sErr);
        }

        protected void dgvList_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (e.CommandName.ToUpper() == "SELECTED")
            {
                string id = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ID"].ToString();
                BUS.GadgetControl ctr = new BUS.GadgetControl();
                SetDataToForm(ctr.Get(Convert.ToInt32(id), ref sErr));
            }
        }

        private void SetDataToForm(DTO.GadgetInfo gadgetItemsInfo)
        {
            if (gadgetItemsInfo.ID != -1)
                txtID.Text = gadgetItemsInfo.ID.ToString();
            txtName.Text = gadgetItemsInfo.Title;
            cboType.Text = gadgetItemsInfo.Type;
            txtWidth.Value = gadgetItemsInfo.Width;
            txtHeigth.Value = gadgetItemsInfo.Height;
            imgPic.ImageUrl = "~/Upload/" + gadgetItemsInfo.Image;
        }
    }
}
